package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import DBTasks.FosterHomeAssignment;
import DBTasks.FosterInformation;
import DBTasks.PetInformation;

@SuppressWarnings("serial")
public class FosterPanel extends JPanel implements ActionListener{

	private JButton submitButton;
	private ArrayList<String[]> namesAndPhones;
	private String shelterName;
	private MainFrame frame;
	private JComboBox idList, nameList;
	private JButton returnButton;

	public FosterPanel(MainFrame mainFrame) {
	
		this.setSize(550,600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
	    shelterName = (String) frame.getLogin().getShelterList().getSelectedItem();
		JLabel assignLabel = new JLabel("Assign Pet from " + shelterName + " to a Foster Home");
		assignLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		submitButton = new JButton("Submit Assignment");
		submitButton.addActionListener(this);
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		JLabel id = new JLabel("Pet ID:");
		JLabel name = new JLabel("Name - Phone:");
		
		idList = new JComboBox(PetInformation.listPetInfo(shelterName).toArray());
		idList.setSelectedIndex(0);
		idList.addActionListener(this);
		
		namesAndPhones = FosterInformation.listFosterInfo(); 
		nameList = new JComboBox();
		for(int i = 0; i < namesAndPhones.size(); i++) {
			String[] fosterer = namesAndPhones.get(i);
			nameList.addItem(fosterer[0] + " - " + fosterer[1]);
		}
		nameList.setSelectedIndex(0);
		nameList.addActionListener(this);

		GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addComponent(assignLabel)
				.addGroup(gl.createSequentialGroup()
						.addGap(100)
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(id)
								.addComponent(idList))
						.addGap(30)
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(name)
								.addComponent(nameList))
						.addGap(100)
								)
				.addGroup(gl.createSequentialGroup()
						.addComponent(submitButton)
						.addGap(20)
						.addComponent(returnButton)));
		
		gl.linkSize(SwingConstants.VERTICAL, idList, nameList);
		
		gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addComponent(assignLabel)
			    .addGap(30)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
			        .addGroup(gl.createSequentialGroup()
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
			                .addComponent(id)
			                .addComponent(name))
			            .addGap(5)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
							.addComponent(idList)
						    .addComponent(nameList))
						.addGap(20))
			     )
			     .addGap(15)
				 .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						 .addComponent(submitButton)
						 .addComponent(returnButton))
				 .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		gl.setAutoCreateGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource().equals(submitButton)) {
			String[] nameAndPhone = namesAndPhones.get(nameList.getSelectedIndex());
			if(FosterHomeAssignment.assign(Integer.parseInt((String) idList.getSelectedItem()), 
							    shelterName, nameAndPhone[0], nameAndPhone[1])) {
				frame.setEmployeePanels();
				frame.setPanel(frame.getHomepage());
				System.out.println("Pet " + idList.getSelectedItem() + " has been assigned to " + nameAndPhone[0] + "'s foster home.");
			}
			else {
				System.out.println("Invalid Entry. Try Again.");
			}
        }
		
		if(e.getSource().equals(returnButton)) {
			frame.setPanel(frame.getHomepage());
		}
		
	}

}
